import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_easyrefresh/material_footer.dart';
import 'package:flutter_easyrefresh/material_header.dart';
import 'package:flutterappmydemo/head/classical_two_header.dart';
import 'package:toast/toast.dart';

class ListViewExample extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _ExampleListViewState();
  }
}

class _ExampleListViewState extends State<ListViewExample> {
  EasyRefreshController _controller;

  @override
  void initState() {
    super.initState();
    _controller = EasyRefreshController();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('列表展示'),
      ),
      body: EasyRefresh.custom(
        controller: _controller,
        enableControlFinishRefresh: false,
        enableControlFinishLoad: true,
        // bottomBouncing: true,
        //_localizations
        /* header: ClassicalHeader(
          enableInfiniteRefresh: false,
          bgColor: Colors.white,
          //Theme.of(context).primaryColor
          infoColor: Colors.grey[700],
          textColor: Colors.grey[700],
          float: false,
          //是否浮动
          enableHapticFeedback: false, //开启震动反馈
        ),

        footer: ClassicalFooter(
          infoColor: Colors.grey[700],
          textColor: Colors.grey[700],
          enableHapticFeedback: false,
        ),*/

        header: ClassicalTwoHeader(),
        footer: ClassicalFooter(
          //关闭该功能，没有更多数据时，脚布局回隐藏
          enableInfiniteLoad: false
        ),

        onRefresh: () async {
          await Future.delayed(Duration(seconds: 2), () {
            print('ListViewExample.build onRefresh');
            setState(() {
              // _count = 20;
            });
            _controller.resetLoadState();
            _controller.finishRefresh();
          });
        },

        //onRefresh: _enableRefresh ? () async {} : null,

        onLoad: () async {
          print('ListViewExample.build onLoad');
          await Future.delayed(Duration(seconds: 2), () {
            print('onLoad');
            setState(() {
              // _count += 10;
            });
            _controller.finishLoad(noMore: true, success: true);
          });
        },

        slivers: <Widget>[
          SliverList(
            delegate: SliverChildBuilderDelegate(
              (context, index) {
                return InkWell(
                  onTap: () {
                    /*Toast.show(getData()[index] + "", context,
                      duration: Toast.LENGTH_SHORT, gravity: Toast.BOTTOM);*/

                    Toast.show(getData()[index] + "", context);
                  },
                  child: Card(
                    child: new Container(
                      alignment: Alignment.centerLeft,
                      margin: EdgeInsets.all(10.0),
                      child: Row(
                        children: <Widget>[
                          ClipRRect(
                            borderRadius: BorderRadius.circular(5),
                            child: new Image.network(
                              "http://image.test.laodao.so/image/news/content/598a4e30-3383-11ea-b72b-15f836b446c5.jpeg",
                              fit: BoxFit.cover,
                              height: 100,
                              width: 100,
                            ),
                          ),
                          new Container(
                            margin: EdgeInsets.only(left: 10.0),
                            height: 100,
                            child: Column(
                              children: <Widget>[
                                Text(getData()[index]),
                                Text(getData()[index]),
                                Text(getData()[index]),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                );
              },
              childCount: getData().length,
            ),
          ),
        ],
      ),
    );
  }
}

List<String> getData() {
  var list = new List<String>();
  for (int i = 0; i < 30; i++) {
    list.add("ypk2=$i");
  }
  return list;
}

/*Container(
                  //height: double.infinity,
                  child: new ListView.builder(
                    itemBuilder: (context, index) {
                      return InkWell(
                        onTap: () {
                          /*Toast.show(getData()[index] + "", context,
                      duration: Toast.LENGTH_SHORT, gravity: Toast.BOTTOM);*/

                          Toast.show(getData()[index] + "", context);
                        },
                        child: Card(
                          child: new Container(
                            alignment: Alignment.centerLeft,
                            margin: EdgeInsets.all(10.0),
                            child: Row(
                              children: <Widget>[
                                ClipRRect(
                                  borderRadius: BorderRadius.circular(5),
                                  child: new Image.network(
                                    "http://image.test.laodao.so/image/news/content/598a4e30-3383-11ea-b72b-15f836b446c5.jpeg",
                                    fit: BoxFit.cover,
                                    height: 100,
                                    width: 100,
                                  ),
                                ),
                                new Container(
                                  margin: EdgeInsets.only(left: 10.0),
                                  height: 100,
                                  child: Column(
                                    children: <Widget>[
                                      Text(getData()[index]),
                                      Text(getData()[index]),
                                      Text(getData()[index]),
                                    ],
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ),
                      );
                    },
                    itemCount: getData().length,
                  ),
                ); */
